package bdsimple.impl.mysql;

import java.sql.ResultSet;

import bdsimple.abstractfactory.AbstractFactoryDAO;
import bdsimple.dao.base.TarjetaMemoriaDAO;
import bdsimple.dao.base.TarjetaMemoriaVampiroDAO;
import bdsimple.dao.base.VampiroDAO;
import bdsimple.dto.base.TarjetaMemoriaDTO;
import bdsimple.dto.base.TarjetaMemoriaVampiroDTO;
import bdsimple.dto.base.VampiroDTO;
import dao.base.api.IDTO;
import dao.base.impl.Reference;

public class TarjetaMemoriaVampiroDAOImpl extends MySQLBaseDAO implements TarjetaMemoriaVampiroDAO {

	public TarjetaMemoriaVampiroDAOImpl() {
		super(TarjetaMemoriaVampiroDTO.class);
	}

	@Override
	protected String createInsertCollst(IDTO dto) throws Exception {
		StringBuffer strbuf = new StringBuffer();

		strbuf.append(TarjetaMemoriaVampiroDTO.TARJETA_MEMORIA_REFERENCIA);
		strbuf.append(", ");
		strbuf.append(TarjetaMemoriaVampiroDTO.VAMPIRO_REFERENCIA);

		return strbuf.toString();
	}

	@Override
	protected String createInsertValues(IDTO dto) throws Exception {
		
		TarjetaMemoriaVampiroDTO tarjetaMemoriaVampiroDTO = (TarjetaMemoriaVampiroDTO) dto;

		StringBuffer strbuf = new StringBuffer();
		
		Reference<VampiroDTO> refVampiro = tarjetaMemoriaVampiroDTO.getVampiroDTORef(); 
	    refVampiro.checkInsert();
	    strbuf.append(refVampiro.getIdAsString());
	    
		strbuf.append(", ");
		
		Reference<TarjetaMemoriaDTO> refTarjetaMemoria = tarjetaMemoriaVampiroDTO.getTarjetaMemoriaDTORef();
	    refTarjetaMemoria.checkInsert();
	    strbuf.append(refTarjetaMemoria.getIdAsString());

		return strbuf.toString();
		
	}

	@Override
	protected String createTableColumns() throws Exception {
		
		VampiroDAO vampiroDAO = (VampiroDAO) //
			AbstractFactoryDAO.getFactoryDAO().getDAO(//
				VampiroDAO.class, connectionBean);
		
		TarjetaMemoriaDAO tarjetaMemoriaDAO = (TarjetaMemoriaDAO) //
			AbstractFactoryDAO.getFactoryDAO().getDAO(//
					TarjetaMemoriaDAO.class, connectionBean);
		
		StringBuffer strbuf = new StringBuffer();
		
		strbuf.append(TarjetaMemoriaVampiroDTO.ID);
		strbuf.append(" INT PRIMARY KEY AUTO_INCREMENT, ");
		strbuf.append(TarjetaMemoriaVampiroDTO.VAMPIRO_REFERENCIA);
		strbuf.append(" INT REFERENCES ");
		strbuf.append(vampiroDAO.getTableName());
		strbuf.append(", ");
		strbuf.append(TarjetaMemoriaVampiroDTO.TARJETA_MEMORIA_REFERENCIA);
		strbuf.append(" INT REFERENCES ");
		strbuf.append(tarjetaMemoriaDAO.getTableName());
		
		return strbuf.toString();
		
	}

	@Override
	protected String createUpdateValues(IDTO dto) throws Exception {
		
		TarjetaMemoriaVampiroDTO tarjetaMemoriaVampiroDTO = (TarjetaMemoriaVampiroDTO) dto;

		StringBuffer strbuf = new StringBuffer();
		
		strbuf.append(TarjetaMemoriaVampiroDTO.VAMPIRO_REFERENCIA);
		strbuf.append(" = ");
		
		Reference<VampiroDTO> refVampiro = tarjetaMemoriaVampiroDTO.getVampiroDTORef();
	    refVampiro.checkUpdate();
	    strbuf.append(refVampiro.getIdAsString());
	    
	    strbuf.append(", ");
	    
	    strbuf.append(TarjetaMemoriaVampiroDTO.TARJETA_MEMORIA_REFERENCIA);
		strbuf.append(" = ");
		
		Reference<TarjetaMemoriaDTO> refTarjetaMemoria = tarjetaMemoriaVampiroDTO.getTarjetaMemoriaDTORef();
		refTarjetaMemoria.checkUpdate();
	    strbuf.append(refTarjetaMemoria.getIdAsString());
		
		return strbuf.toString();
	}

	@Override
	protected IDTO internalResultSetToDTO(ResultSet rs, IDTO dto)
			throws Exception {
		TarjetaMemoriaVampiroDTOImpl ret = (TarjetaMemoriaVampiroDTOImpl) dto;
		
		ret.setId(rs.getInt(TarjetaMemoriaVampiroDTO.ID));
		
		Reference<VampiroDTO> refVampiro = ret.getVampiroDTORef();
	    refVampiro.setRefIdent(rs.getInt(TarjetaMemoriaVampiroDTO.VAMPIRO_REFERENCIA));
	    refVampiro.setRefValue(null);
	    
	    Reference<TarjetaMemoriaDTO> refTarjetaMemoria = ret.getTarjetaMemoriaDTORef();
	    refTarjetaMemoria.setRefIdent(rs.getInt(TarjetaMemoriaVampiroDTO.TARJETA_MEMORIA_REFERENCIA));
	    refTarjetaMemoria.setRefValue(null);
		
		return ret;
	}

}

